<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="detailList"
    >
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="SN码(序列号)" align="center" prop="snCode" /> -->
      <el-table-column
        label="物料序列号"
        align="center"
        prop="materialSerial"
      />
      <el-table-column label="物料编码" align="center" prop="productCode" />
      <el-table-column label="物料名称" align="center" prop="productName" />
      <el-table-column label="规格" align="center" prop="productSpec" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="数量" align="center" prop="itemAmount">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.itemAmount"
            placeholder="请输入数量"
            v-if="optType !== 'check'"
          ></el-input>
          <span v-if="optType === 'check'">{{ scope.row.itemAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row, scope.$index)"
            v-if="optType == 'add'"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="optType == 'update'"
            @click="handleDelete(scope.row, scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  listDetail,
  getDetail,
  delDetail,
  addDetail,
  updateDetail,
  listByMaterialSerial,
  listAllDetail,
  delDelete,
} from "@/api/mes/pro/productBackDetail";

export default {
  name: "Detail",
  props: ["optType", "parentCode", "parentId"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产退料单结果表格数据
      detailList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        backId: this.parentId,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialSerial: [
          { required: true, message: "物料序列号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产退料单结果列表 */
    getList() {
      if (this.optType !== "add") {
        this.loading = true;
        listAllDetail(this.queryParams).then((response) => {
          this.detailList = response.data;
          this.loading = false;
          this.$emit("onSelected", this.detailList);
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {},
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加时一个个扫码添加操作 */
    refreshBackList(data) {
      let reqParam = {
        materialSerial: data.materialSerial,
        workorderCode: data.workorderCode,
      };
      listByMaterialSerial(reqParam).then((response) => {
        if (response.code == 200) {
          if (this.detailList.length) {
            let boolean = this.detailList.some((item) => item.materialSerial == response.data.materialSerial);
            console.log(boolean,'boolean');
            if (!boolean) {
              this.detailList.push(response.data);
              this.loading = false;
            }else{}
          } else {
            this.detailList.push(response.data);
            this.loading = false;
          }
          this.$emit("onSelected", this.detailList);
        }
      });
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map((item) => item.id);
    //   this.single = selection.length !== 1;
    //   this.multiple = !selection.length;
    // },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/detail/export",
        {
          ...this.queryParams,
        },
        `detail_${new Date().getTime()}.xlsx`
      );
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   console.info("selection===", selection);
    //   this.ids = selection.map((item) => item.id);
    //   this.single = selection.length !== 1;
    //   this.multiple = !selection.length;
    //   this.detailList = selection;
    //   this.$emit("onSelected", this.detailList);
    // },
    //删除
    handleDelete(row, index) {
      // if (this.optType == "update") {
      //   let materialSerial = row.materialSerial;
      //   let backId = this.queryParams.backId;
      //   let ids = [];
      //   ids.push(row.id);
      //   this.$modal
      //     .confirm('是否确认删除生产退料单结果编号为"' + materialSerial + '"的数据项？')
      //     .then(function () {
      //       return delDelete(backId, ids);
      //     }).then(() => {
      //       this.getList();
      //       this.$modal.msgSuccess("删除成功");
      //       this.$emit("onSelected", this.detailList);
      //     }).catch(() => {});
      // }else if (this.optType == "add"){
      this.detailList.splice(index, 1);
      this.$emit("onSelected", this.detailList);
      // }
    },
  },
};
</script>
